package com.zq.it.service.impl.bbs;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.zq.it.dao.bbs.PostDao;
import com.zq.it.dao.bbs.ResponseDao;
import com.zq.it.dao.bbs.TypeDao;
import com.zq.it.pojo.Page;
import com.zq.it.pojo.Post;
import com.zq.it.pojo.Response;
import com.zq.it.pojo.Type;
import com.zq.it.service.bbs.BbsService;
import com.zq.it.tool.BbsConstants;

public class BbsServiceImpl<T> implements BbsService<T> {

    @Resource
    private PostDao postDao;
    @Resource
    private ResponseDao responseDao;
    @Resource
    private TypeDao typeDao;

    @Override
    public void addObject(T object) {
        if (object instanceof Post)
            postDao.add((Post) object);
        else if (object instanceof Response)
            responseDao.add((Response) object);
        else if (object instanceof Type)
            typeDao.add((Type) object);
    }
    @Override
	public int getPostsCount(String firstDate, String secondDate) {
    	return postDao.getCountByHql("select count(*) from Post as p where p.createTime between '" + secondDate + "' and '" + firstDate+"'");
    }
    @Override
	public List<Type> findTypes() {
        return typeDao.findAll("from com.zq.it.pojo.Type");
    }

    @Override
    public void updaObject(T object) {
        if (object instanceof Post)
            postDao.update((Post) object);
        else if (object instanceof Response)
            responseDao.update((Response) object);
        else if (object instanceof Type)
            typeDao.update((Type) object);
    }

    @Override
    public void deleteObject(T object) {
        if (object instanceof Post)
            postDao.delete((Post) object);
        else if (object instanceof Response) {
        	Response resp = (Response) object;
        	responseDao.delete(resp);
//        	Post post = postDao.getById(resp.getPost().getId(), Post.class);
//        	post.reduceRespCount(1);
//        	postDao.update(post);
        }
        else if (object instanceof Type)
            typeDao.delete((Type) object);
    }

    @Override
	public Map findMyPosts(String parameter, Object value, Page page) {
        Map etyMap = new HashMap();
        etyMap.put(BbsConstants.ETY_TOTALCOUNT, postDao.getCountByHql("select count(*) from com.zq.it.pojo.Post as p where p.userName=" + value));
        etyMap.put(BbsConstants.ETY_LIST, postDao.findByCriteria(Post.class, "responses", parameter, value, page));
        return etyMap;
    }

    @Override
	@SuppressWarnings("unchecked")
    public Map findResps(Page page, long id) {
        Map etyMap = new HashMap();
        etyMap.put(
                BbsConstants.ETY_TOTALCOUNT,
                typeDao.getCountByHql("select count(*) from com.zq.it.pojo.Response as resp where resp.post.id="
                        + id));
        etyMap.put(BbsConstants.ETY_LIST, typeDao.findByHql(page,
                "from com.zq.it.pojo.Response as resp where resp.post.id=" + id
                        + " order by resp.responseTime desc"));
        return etyMap;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Map findTypes(Page page, String name) {
        Map etyMap = new HashMap();
        etyMap.put(BbsConstants.ETY_TOTALCOUNT, typeDao.getCount(
                "select count(*) from com.zq.it.pojo.Type as t ", "name", name));
        etyMap.put(BbsConstants.ETY_LIST, typeDao.getList(page,
                "from com.zq.it.pojo.Type as t ", "name", name));
        return etyMap;
    }

    @Override
	@SuppressWarnings("unchecked")
    public T getById(long id, Class<T> c) {
        return (T) responseDao.getById(id, c);
    }

	@Override
	public int addType(Type type) {
		// TODO Auto-generated method stub
		return typeDao.save(type);
	}
}
